package leetcodecompetition.D340;

import org.junit.jupiter.api.Test;

import java.util.Arrays;

public class T6359 {
    int [] nums;
    int p;
    public int minimizeMax(int[] nums, int p) {
        Arrays.sort(nums);
        this.nums=nums;
        this.p=p;
        int left=-1;
        int right=nums[nums.length-1]-nums[0];
        while(left+1<right){
            int mid=(left+right)/2;
            if (check(mid)){
                right=mid;
            }else {
                left=mid;
            }
        }
        return right;
    }
    public boolean check(int max) {
        int left=0;
        int cnt=0;
        while(left<nums.length-1){
            if (nums[left+1]-nums[left]<=max){
                left+=2;
                cnt++;
            }else{
                left++;
            }
        }
        return cnt>=p;
    }

    @Test
    public void test() {
        int[] ints = {1, 1, 2, 3, 7,10};
        System.out.println(minimizeMax(ints, 2));

    }
}
